package com.myproject0616chat;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import javax.swing.JTextArea;

public class Receriver {
	private JTextArea jta;
	Socket socket;
	Font font=new Font("微软雅黑", Font.BOLD, 80);

	public Receriver(JTextArea jta) {
		this.jta = jta;
		socket = SocketUtil.getSocket();
	}

	public void startReceriver() {
		new Thread(new Runnable() {
			public void run() {
				socket = SocketUtil.getSocket();
				while (true) {

					try {
						ObjectInputStream iis = new ObjectInputStream(socket.getInputStream());
						Object mess = iis.readObject();
						String str = (String) mess;
						jta.append(str+"\n");
						jta.setCaretPosition(jta.getText().length());//设置光标到JTextArea中的最后一个字符(滚动条始终在最后一行)
					} catch (Exception e) {
						jta.append("服务器故障\n");
						jta.setFont(font);
						jta.setDisabledTextColor(Color.red);
						System.out.println("\n"+"服务器故障"+"\n");
						jta.setCaretPosition(jta.getText().length());
						break;
					}

				}
			}
		}).start();
	}
}
